package cn.com.egova.erp.action;

import cn.com.egova.erp.biz.IOrdersBiz;
import cn.com.egova.erp.entity.Emp;
import cn.com.egova.erp.entity.Orderdetail;
import cn.com.egova.erp.entity.Orders;
import cn.com.egova.erp.exception.ErpException;
import cn.com.egova.erp.utils.FastJsonUtil;
import com.alibaba.fastjson.JSON;
import com.redsun.bos.ws.Waybilldetail;
import org.apache.shiro.authz.UnauthorizedException;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 订单Action
 *
 * @author Administrator
 */
public class OrdersAction extends BaseAction<Orders> {
    private IOrdersBiz ordersBiz;

    private String json;

    private Long waybillsn;

    public String getJson() {
        return json;
    }

    public void setJson(String json) {
        this.json = json;
    }

    public void setOrdersBiz(IOrdersBiz ordersBiz) {
        this.ordersBiz = ordersBiz;
        super.setBaseBiz(this.ordersBiz);
    }

    public Long getWaybillsn() {
        return waybillsn;
    }

    public void setWaybillsn(Long waybillsn) {
        this.waybillsn = waybillsn;
    }

    @Override
    public void add() {
        try {
            Emp loginUser = getLoginUser();
            if (loginUser == null) {
                //未登录或session失效
                ajaxReturn(false, "请先登录!");
                return;
            }
            Orders orders = getT();
            //订单创建者
            orders.setCreater(loginUser.getUuid());
            //订单明细
            List<Orderdetail> detailList = JSON.parseArray(json, Orderdetail.class);
            orders.setOrderDetails(detailList);
            ordersBiz.add(orders);
            ajaxReturn(true, "添加订单成功!");
        } catch (ErpException e) {
            ajaxReturn(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ajaxReturn(false, "添加订单失败!");
        }
    }


    /**
     * 订单审核
     */
    public void doCheck() {
        Emp loginUser = getLoginUser();
        if (loginUser == null) {
            ajaxReturn(false, "请先登录!");
            return;
        }
        try {
            //getId()的id参数是从前端传入的订单的uuid
            ordersBiz.doCheck(getId(), loginUser.getUuid());
            ajaxReturn(true, "审核成功!");
        } catch (UnauthorizedException e) {
            ajaxReturn(false, "权限不足");
        } catch (ErpException e) {
            ajaxReturn(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ajaxReturn(false, "审核失败!");
        }
    }


    /**
     * 订单确认
     */
    public void doStart() {
        Emp loginUser = getLoginUser();
        if (loginUser == null) {
            ajaxReturn(false, "请先登录!");
            return;
        }
        try {
            //getId()的id参数是从前端传入的订单的uuid
            ordersBiz.doStart(getId(), loginUser.getUuid());
            ajaxReturn(true, "确认成功!");
        } catch (ErpException e) {
            ajaxReturn(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ajaxReturn(false, "确认失败!");
        }
    }


    /**
     * 我的订单
     */
    public void myListByPage() {
        if (null == getT1()) {
            //构建查询条件
            setT(new Orders());
        }
        //登录用户编号查询
        Emp loginUser = getLoginUser();
        getT1().setCreater(loginUser.getUuid());
        super.listByPage();
    }


    /**
     * 导出订单
     */
    public void export() {
        String fileName = "Orders_" + getId() + ".xls";
        HttpServletResponse response = ServletActionContext.getResponse();
        try {
            //设置输出流，实现下载，指定是excel,因为文件名有中文，故还要解决中文乱码
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(), "ISO-8859-1"));
            ordersBiz.export(response.getOutputStream(), getId());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 导出订单
     */
    public void exportByTemplate() {
        String fileName = "Orders_" + getId() + ".xls";
        HttpServletResponse response = ServletActionContext.getResponse();
        try {
            //设置输出流，实现下载，指定是excel,因为文件名有中文，故还要解决中文乱码
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(), "ISO-8859-1"));
            ordersBiz.exportByTemplate(response.getOutputStream(), getId());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 根据运单号获取运单详情
     */
    public void waybilldetailList() {
        try {
            List<Waybilldetail> waybilldetailList = ordersBiz.waybilldetailList(waybillsn);
            FastJsonUtil.ajaxReturn(true, "获取运单详情成功!", waybilldetailList);
        } catch (Exception e) {
            e.printStackTrace();
            FastJsonUtil.ajaxReturn(false, "获取运单详情失败!", null);
        }
    }
}
